package com.woorigil.social.domain;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;

@Repository
public abstract class SqlMapDaoSupport<T, I extends Serializable> extends SqlMapClientDaoSupport implements BaseDao<T, I> {
	abstract public String getNamespace();

	@Autowired
	public void setMainSqlMapClient(SqlMapClient sqlMapClient) {
		this.setSqlMapClient(sqlMapClient);
	}
	
	public String getStatementName(String namespace, String id) {
		return StringUtils.trimToEmpty(namespace) + "." + StringUtils.trimToEmpty(id);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public I insert(T entity) {
		return (I)getSqlMapClientTemplate().insert(getNamespace() + ".insert", entity);
	}

	@Override
	public void update(T entity) {
		getSqlMapClientTemplate().update(getNamespace() + ".update", entity);
	}

	@Override
	public void delete(I id) {
		getSqlMapClientTemplate().delete(getNamespace() + ".delete", id);			
	}

	@SuppressWarnings("unchecked")
	@Override
	public T select(I id) {
		return (T)getSqlMapClientTemplate().queryForObject(getNamespace() + ".select", id);		
	}
}
